package practice01;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice01
 * @ClassName: StudentManager
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/16 18:22
 * @Copyright 大牛版板所有
 */
public class StudentManager {
    static ArrayList<Student> studentList = new ArrayList<>();

    static {
        studentList.add(new Student("端木风",19,1000,"341225199811237170"));
        studentList.add(new Student("东方云",20,1001,"341225199716737156"));
        studentList.add(new Student("西门音",19,1002,"341225199811467890"));
        studentList.add(new Student("欧阳天",21,1003,"341225199614327152"));
    }

    // 显示全部学员信息
    public static void showAllInfo(){
        System.out.println("=============学生信息表=================");
        System.out.println(" 姓名\t年龄\t学号\t身份证");
        for (int i = 0; i < studentList.size(); i++) {
            Student s = studentList.get(i);
            System.out.println(" "+s.getName()+"\t"+s.getAge()+"\t\t"+s.getStuNo()+"\t"+s.getIdentityCard());
        }
    }

    // 确认学号是否唯一
    public static boolean exist(int stuNo){
        for (int i = 0; i < studentList.size(); i++) {
            if (stuNo == studentList.get(i).getStuNo()){
                return true;
            }
        }
        return false;
    }

    // 查找下标
    public static int getIndex(int stuNo){
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            if (stuNo == studentList.get(i).getStuNo()){
                index = i;
                break;
            }
        }
        return index;
    }

    // 修改学员信息
    public static boolean updateStudent(Student student){
        if (exist(student.getStuNo())){
            int index = getIndex(student.getStuNo());
            if (index != -1){
                studentList.set(index,student);
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    // 增加学员信息
    public static boolean addStudent(Student student){
        if (!exist(student.getStuNo())){
            studentList.add(student);
            return true;
        }
        return false;
    }

    // 删除学员信息
    public static boolean deleteStudent(int stuNo){
        int index = getIndex(stuNo);
        if (index != -1){
            studentList.remove(index);
            return true;
        }
        else {
            return false;
        }
    }


    public static void main(String[] args) {
        Scanner input  = new Scanner(System.in);
        boolean isExit = true;
        do {
            System.out.println("===================学员信息表======================");
            System.out.println("---------------1、显示全部学员信息----------------");
            System.out.println("-----------------2、删除学员信息------------------");
            System.out.println("-----------------3、修改学员信息------------------");
            System.out.println("-----------------4、增加学员信息------------------");
            System.out.println("-----------------5、退出学员系统------------------");
            System.out.print("请输入你选择的功能：");
            int type = input.nextInt();
            switch (type){
                case 1:
                    showAllInfo();
                    break;
                case 2:
                    System.out.println("请输入要删除的学员学号：");
                    int id = input.nextInt();
                    boolean isDelete = deleteStudent(id);
                    if(isDelete){
                        System.out.println("删除学员信息成功！");
                    }
                    else{
                        System.out.println("删除学员信息失败！");
                    }
                    break;
                case 3:
                    Student student1 = new Student();
                    System.out.print("请输入学号:");
                    student1.setStuNo(input.nextInt());
                    System.out.print("请输入姓名:");
                    student1.setName(input.next());
                    System.out.print("请输入身份证号码:");
                    student1.setIdentityCard(input.next());
                    System.out.print("请输入年龄:");
                    student1.setAge(input.nextInt());
                    boolean isUpdate = updateStudent(student1);
                    if(isUpdate){
                        System.out.println("修改学员信息成功！");
                    }
                    else{
                        System.out.println("修改学员信息失败！");
                    }
                    break;
                case 4:
                    Student student2 = new Student();
                    System.out.print("请输入学号:");
                    student2.setStuNo(input.nextInt());
                    System.out.print("请输入姓名:");
                    student2.setName(input.next());
                    System.out.print("请输入身份证号码:");
                    student2.setIdentityCard(input.next());
                    System.out.print("请输入年龄:");
                    student2.setAge(input.nextInt());
                    boolean isSuc = addStudent(student2);
                    if(isSuc){
                        System.out.println("新增学员信息成功！");
                    }
                    else{
                        System.out.println("新增学员信息失败！");
                    }
                    break;
                case 5:
                    isExit = false;
                    break;
                default:
                    System.out.println("你选择的功能不存在！");
                    isExit = false;
                    break;
            }
            if (!isExit){
                System.out.println("退出程序，谢谢使用学生管理系统2.0!");

            }
        }while (isExit);
    }
}
